// 实现这个项目的构建任务
// src dest 文件拷贝
// parallel 并行加载
// series 串行加载
// watch 监听文件变化
const { src, dest, parallel, series, watch } = require('gulp')

const del = require('del') // 清除文件
const browserSync = require('browser-sync') // 热更新
const loadPlugins = require('gulp-load-plugins') //自动加载插件
const plugins = loadPlugins()
const bs = browserSync.create() // 创建开发服务器
// const plugins.sass = require('gulp-sass') // 样式编译
// const plugins.babel = require('gulp-babel') // 脚本编译
// const plugins.swig = require('gulp-swig') // 页面模板编译
// const plugins.imagemin = require('gulp-imagemin') // 图片字体编译
const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}


const clean = () => {
  return del(['dist', 'temp'])
}
const style = () => {
  // base为基准路径, outputStyle: 'expanded'样式完全展开
  return src('src/assets/styles/*.scss', { base: 'src' })
         .pipe(plugins.sass({ outputStyle: 'expanded' }))
         .pipe(dest('temp')).pipe(bs.reload({ stream: true }))
}

const script = () => {
  return src('src/assets/scripts/*.js', { base: 'src' })
         .pipe(plugins.babel({ presets: ['@babel/preset-env'] }))
         .pipe(dest('temp')).pipe(bs.reload({ stream: true }))
}
// 传递渲染数据  默认不缓存defaults:{ cache: false }
const page = () => {
  return src('src/*.html', { base: 'src' })
         .pipe(plugins.swig({ data, defaults:{ cache: false } }))
         .pipe(dest('temp')).pipe(bs.reload({ stream: true }))
}

const image = () => {
  return src('src/assets/images/**', { base: 'src' })
  .pipe(plugins.imagemin())
  .pipe(dest('dist'))
}

const font = () => {
  return src('src/assets/fonts/**', { base: 'src' })
  .pipe(plugins.imagemin())
  .pipe(dest('dist'))
}

const lint = () => {
  return src('src/assets/scripts/*.js', { base: 'src' })
  .pipe(plugins.eslint())
  .pipe(plugins.eslint.format())
  .pipe(plugins.eslint.failAfterError())
}

// 其他文件拷贝
const extra = () => {
  return src('public/**', { base: 'public' }).pipe(dest('dist'))
}

// gulp-useref 减少依赖文件的使用
// 文件压缩 gulp-clean-css、gulp-htmlmin、gulp-uglify
const useref = () => {
  return src('temp/*.html', { base: 'temp' }).pipe(plugins.useref({ searchPath: ['dist', '.'] }))
        .pipe(plugins.if(/\.js$/, plugins.uglify()))
        .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
        .pipe(plugins.if(/\.html$/, plugins.htmlmin({
          collapseWhitespace: true,
          minifyCSS: true,
          minifyJS: true
        })))
        .pipe(dest('dist'))
}
const serve = () => {
  watch('src/assets/styles/*.scss', style)
  watch('src/assets/scripts/*.js', script)
  watch('src/*.html', page)
  // watch('src/assets/images/**', image)
  // watch('src/assets/fonts/**', font)
  // watch('public/**', extra)
  watch(['src/assets/images/**', 'src/assets/fonts/**', 'public/**'], bs.reload)
  bs.init({
    port: 7070,
    notify: false,
    // files: 'dist/**', .pipe(bs.reload({ stream: true }))
    server: {
      baseDir: ['temp', 'src', 'public'],
      routes: {
        '/node_modules': 'node_modules'
      }
    }
  })
}
const compile = parallel(style, script, page)
const build = series(clean, parallel(series(compile, useref), image, font, extra))
const develop = series(compile, serve)
module.exports = {
  clean,
  serve,
  compile,
  build,
  develop,
  lint
}
